package com.example.hello.service.impl;

import com.example.hello.model.Drink;
import com.example.hello.service.IDrinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import com.example.hello.repository.DrinkRepository;

import java.util.List;
import java.util.Optional;

@Service
public class DrinkJpaService implements IDrinkService {
    //6自动注入repsitory
    @Autowired
    private DrinkRepository drinkRepository;
    @Override
    public Page<Drink> getAllDrinks(PageRequest pageRequest) {
        return drinkRepository.findAll(pageRequest);
    }
    @Override
    public void addNewDrink(Drink drink) {
        drinkRepository.save(drink);
    }

    @Override
    public Drink getDrinkById(Integer id) {
       Optional<Drink> optional= drinkRepository.findById(id);
        if (optional.isPresent()) {
            return optional.get();
        }
        return null;
    }

    @Override
    public void updateDrink(Drink drink) {
        drinkRepository.save(drink);
    }

    @Override
    public void deleteDrink(Integer id) {


        drinkRepository.deleteById(id);
    }

}
